package com.yesorno.ui;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.yesorno.activity.R;
import com.yesorno.utils.BitmapUtils;
import com.yesorno.utils.HttpUtils;

/**
 * 自定义title的activity
 * 
 * @author samoin
 * @date 2011-07-05
 * @version 0.1
 * 
 */
public abstract class CustomTitleActivity extends Activity {

	private LinearLayout TITLE_LAYOUT;// 自定义title对应的布局

	static int TEXT_SIZE = 15;

	/**
	 * 提醒的铃声的播放器
	 */
	public MediaPlayer mMediaPlayer;

	/**
	 * 给title里追加按钮等组件
	 */
	public abstract void setTitleViews();

	/**
	 * 调用setContentView方法设置layout对应的id
	 */
	public abstract void setLayoutId();

	/**
	 * 重构onCreate方法，假如自定义的title布局
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		// 保证屏幕常亮
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setLayoutId();
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		TITLE_LAYOUT = (LinearLayout) findViewById(R.id.common_title);
		setTitleViews();
	}

	/**
	 * 创建一个title里的左侧按钮，附样式
	 * 
	 * @param context
	 *            对应的context
	 * @param textID
	 *            对应文字的资源id
	 * @param leftMargin
	 *            距离左边的距离
	 * @return
	 */
	public static Button createTitleLeftButton(Context context, int textID,
			int leftMargin) {
		Button button = new Button(context);
		button.setText(textID);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.leftMargin = leftMargin;
		button.setLayoutParams(layoutParams);
		button.setBackgroundResource(R.drawable.title_btn_bg1);
		button.setTextColor(Color.WHITE);
		button.setTextSize(TEXT_SIZE);
		LinearLayout ll = createNewLinearLayout(context);
		ll.addView(button);
		ll.setGravity(Gravity.LEFT);
		return button;
	}

	/**
	 * 创建一个title里的右侧按钮，附样式
	 * 
	 * @param context
	 *            对应的context
	 * @param textID
	 *            对应文字的资源id
	 * @param rightMargin
	 *            距离右边的距离
	 * @return
	 */
	public static Button createTitleRightButton(Context context, int textID,
			int rightMargin) {
		Button button = new Button(context);
		button.setText(textID);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.rightMargin = rightMargin;
		button.setLayoutParams(layoutParams);
		button.setBackgroundResource(R.drawable.title_btn_bg1);
		button.setTextColor(Color.WHITE);
		button.setTextSize(TEXT_SIZE);
		LinearLayout ll = createNewLinearLayout(context);
		ll.addView(button);
		ll.setGravity(Gravity.RIGHT);
		return button;
	}

	/**
	 * 创建一个title里的title，附样式
	 * 
	 * @param context
	 *            对应的context
	 * @param titleID
	 *            对应文字的资源id,为0则为空字符串
	 * @param leftMargin
	 *            距离左边的距离
	 * @return
	 */
	public static TextView createTitleDesc(Context context, int titleID,
			int leftMargin) {
		TextView textView = new TextView(context);
		if (titleID != 0) {
			textView.setText(titleID);
		}
		textView.setTextSize(TEXT_SIZE);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.leftMargin = leftMargin;
		textView.setLayoutParams(layoutParams);
		textView.setTextColor(R.color.title_btn_bg);
		TextPaint tp = textView.getPaint();
		tp.setFakeBoldText(true);
		LinearLayout ll = createNewLinearLayout(context);
		ll.addView(textView);
		ll.setGravity(Gravity.CENTER);
		return textView;
	}

	/**
	 * 创建一个layout<br/>
	 * 主要是解决设置margin导致样式不准的问题，通过wedght属性，强制分成N等分
	 * 
	 * @param context
	 * @return
	 */
	private static LinearLayout createNewLinearLayout(Context context) {
		LinearLayout l1 = new LinearLayout(context);
		LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp1.weight = 1;
		l1.setLayoutParams(lp1);
		return l1;
	}

	public LinearLayout getTITLE_LAYOUT() {
		return TITLE_LAYOUT;
	}

	public void setTITLE_LAYOUT(LinearLayout tITLE_LAYOUT) {
		TITLE_LAYOUT = tITLE_LAYOUT;
	}

	public class BitmapThread implements Runnable {

		private String url;
		private ImageView iv;
		private Boolean isCorner;

		public BitmapThread() {

		}

		public BitmapThread(String url, ImageView iv, Boolean isCorner) {
			this.url = url;
			this.iv = iv;
			this.isCorner = isCorner;
		}

		@Override
		public void run() {
			System.out.println("nowThread-bitmap:"
					+ Thread.currentThread().getId());
			Bitmap bitmap = HttpUtils.returnBitMap(CustomTitleActivity.this,
					url);
			if (isCorner) {
				bitmap = BitmapUtils.getRoundedCornerBitmap(bitmap);
			}
			iv.setImageBitmap(bitmap);
		}

	}

	public class ImgSimpleAdapter extends SimpleAdapter {

		public ImgSimpleAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public void setViewImage(ImageView v, String value) {
			if (value != null && !value.equals("null")
					&& value.trim().length() > 0) {
				v.setImageBitmap(null);
				Handler handler = new Handler();
				BitmapThread r = new BitmapThread(value, v, false);
				handler.post(r);
				v.setVisibility(View.VISIBLE);
			} else {
				((View) v.getParent()).setVisibility(View.GONE);
			}
		}

		@Override
		public void setViewText(TextView v, String text) {
			super.setViewText(v, text);
		}

	}

	/**
	 * 删除listView中的内容
	 * 
	 * @param list
	 * @param adapter
	 * @param listView
	 */
	public void deleteListChild(List<Map<String, Object>> list,
			SimpleAdapter adapter, ListView listView) {
		if (list != null) {
			for (int i = list.size() - 1; i >= 0; i--) {
				list.remove(i);
			}
			adapter.notifyDataSetChanged();
			listView.invalidate();
		}
	}

	/**
	 * 网上找的震动的代码
	 */
	public void vibrator() {

		Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		long[] pattern = { 800, 40, 400, 30 }; // OFF/ON/OFF/ON...
		vibrator.vibrate(pattern, -1);// -1不重复，非-1为从pattern的指定下标开始重复
	}

	/**
	 * 网上找的播放铃声的代码
	 */
	public void playRing() {
		// 获取alarm uri
		Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

		// 创建media player
		mMediaPlayer = new MediaPlayer();
		try {
			mMediaPlayer.setDataSource(this, alert);
			final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mMediaPlayer.setLooping(false);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
